package ua.edu.nuos.jeetraining2012.cms.entity;

import ua.edu.nuos.jeetraining2012.cms.util.validation.validinterface.Sequences;

import javax.persistence.*;
import javax.validation.constraints.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * Student entity bean.
 * <p/>
 * Java Entity Bean that store information about student profile.
 *
 * @author Victor N. Ganichev victor.ganichev@gmail.com
 * @version 1.0 Date: 02.05.13 Time: 13:56
 * @since 1.0
 */
@Entity
@Table(name = "students")
public class StudentEntity implements Serializable {

    @Id
    @SequenceGenerator(
            name = "students_id_seq",
            sequenceName = "students_id_seq",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "students_id_seq"
    )
    @Column(name = "id",
            nullable = false,
            insertable = true,
            updatable = false,
            length = 11,
            precision = 0,
            columnDefinition = "SERIAL")
    @NotNull(message = "Student id can't be empty")
    @Min(value = 1, groups = Sequences.class)
    private long id;

    /**
     * FK to users.id
     */
    @OneToOne
    @JoinColumn(name = "user_id", nullable = false, unique = true)
    @NotNull
    private UserEntity userId;

    /**
     * User photo.
     * <p/>
     * Link to user photo.
     * <br/>
     * Optional.
     */
    @Column(name = "photo",length = 512)
    @Basic(optional = true)
    @Size(min = 0, max = 512, message = "Please note: min length is 0 and max is 512")
    private String photo;

    /**
     * Student birthday.
     * <p/>
     * Date of users birthday.
     * <br/>
     * Optional.
     */
    @Column(name = "birthday")
    @Basic(optional = true)
    @Temporal(value = TemporalType.DATE)
    @Past(message = "pastBirthday")
    @NotNull(message = "isEmpty")
    private Date birthday;

    /**
     * Student occupation.
     * <p/>
     * Work, student, talented schoolkid etc.
     * Optional.
     */
    @Column(name = "occupation",length = 1024)
    @Basic(optional = true)
    @Size(min = 0, max = 1024, message = "boundOf")
    private String occupation;

    /**
     * Student education.
     * <p/>
     * Define student education.
     * <br/>
     * Optional.
     */
    @Column(name = "education",length = 1024)
    @Basic(optional = true)
    @Size(min = 0, max = 1024, message = "boundOf")
    @NotNull(message = "isEmpty")
    private String education;

    /**
     * Student's phone number.
     * <p/>
     * Define student's phone number.
     * <br/>
     * Optional.
     */
    @Column(name = "phone",length = 13)
    @Basic(optional = true)
    @Size(min = 0, max = 13, message = "boundOf")
    @Pattern(regexp = "\\+?[0-9]{12}", message = "incFormat")
    @NotNull(message = "isEmpty")
    private String phone;


    /**
     * Student's Skype.
     * <p/>
     * Define student's Skype login.
     * <br/>
     * Optional.
     */
    @Column(name = "skype",length =256)
    @Basic(optional = true)
    @Size(min = 0, max = 256, message = "boundOf")
    private String skype;


    /**
     * Email notice.
     * <p/>
     * User willing about receiving e-mail notice.
     * <br/>
     * Default: true.
     */
    @Column(name = "receive_notice",nullable = false,columnDefinition = "BOOLEAN DEFAULT 'true'")
    @Basic(optional = false)
    private boolean receiveEmailNotice = true;

    // Default constructor
    public StudentEntity() {}

    // Getters & Setters
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public UserEntity getUserId() {
        return userId;
    }

    public void setUserId(UserEntity userId) {
        this.userId = userId;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getOccupation() {
        return occupation;
    }

    public void setOccupation(String occupation) {
        this.occupation = occupation;
    }

    public String getEducation() {
        return education;
    }

    public void setEducation(String education) {
        this.education = education;
    }

    public boolean isReceiveEmailNotice() {
        return receiveEmailNotice;
    }

    public void setReceiveEmailNotice(boolean receiveEmailNotice) {
        this.receiveEmailNotice = receiveEmailNotice;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getSkype() {
        return skype;
    }

    public void setSkype(String skype) {
        this.skype = skype;
    }

    // Overrides


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        StudentEntity that = (StudentEntity) o;

        if (id != that.id) return false;
        if (receiveEmailNotice != that.receiveEmailNotice) return false;
        if (birthday != null ? !birthday.equals(that.birthday) : that.birthday != null) return false;
        if (education != null ? !education.equals(that.education) : that.education != null) return false;
        if (occupation != null ? !occupation.equals(that.occupation) : that.occupation != null) return false;
        if (phone != null ? !phone.equals(that.phone) : that.phone != null) return false;
        if (photo != null ? !photo.equals(that.photo) : that.photo != null) return false;
        if (skype != null ? !skype.equals(that.skype) : that.skype != null) return false;
        if (userId != null ? !userId.equals(that.userId) : that.userId != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (id ^ (id >>> 32));
        result = 31 * result + (userId != null ? userId.hashCode() : 0);
        result = 31 * result + (photo != null ? photo.hashCode() : 0);
        result = 31 * result + (birthday != null ? birthday.hashCode() : 0);
        result = 31 * result + (occupation != null ? occupation.hashCode() : 0);
        result = 31 * result + (education != null ? education.hashCode() : 0);
        result = 31 * result + (phone != null ? phone.hashCode() : 0);
        result = 31 * result + (skype != null ? skype.hashCode() : 0);
        result = 31 * result + (receiveEmailNotice ? 1 : 0);
        return result;
    }

    @Override
    public String toString() {
        return "StudentEntity{" +
                "id=" + id +
                ", userId=" + userId +
                ", photo='" + photo + '\'' +
                ", birthday=" + birthday +
                ", occupation='" + occupation + '\'' +
                ", education='" + education + '\'' +
                ", phone='" + phone + '\'' +
                ", skype='" + skype + '\'' +
                ", receiveEmailNotice=" + receiveEmailNotice +
                '}';
    }
}
